package com.wwwarehouse.taskcenter.fragment.job_point.step2.child;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.view.animation.Animation;
import android.widget.ListView;

import com.alibaba.fastjson.JSON;
import com.wwwarehouse.common.activity.base.BaseFragment;
import com.wwwarehouse.common.bean.response.CommonClass;
import com.wwwarehouse.common.custom_widget.state_layout.StateLayout;
import com.wwwarehouse.common.http.NoHttpUtils;
import com.wwwarehouse.taskcenter.R;
import com.wwwarehouse.taskcenter.adapter.job_point.BusinessAdapter;
import com.wwwarehouse.taskcenter.bean.job_point.BusinessInfoResponseBean;
import com.wwwarehouse.taskcenter.constant.TaskCenterConstant;
import com.wwwarehouse.taskcenter.constant.job_point.TaskCenterCommon;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import static com.wwwarehouse.taskcenter.fragment.job_point.step2.child.ChooseAddWayChildUseRightByBusinessFragment.KEY_SEARCH_BY_NAME;
import static com.wwwarehouse.taskcenter.fragment.job_point.step2.child.ChooseAddWayChildUseRightByBusinessFragment.KEY_SEARCH_BY_SORT;

/**
 * Created by Jackie on 2017/10/27
 * 子作业点使用权维度  选择添加方式  选择业务单位添加  详情列表
 */

public class ChooseAddWayChildUseRightByBusinessDetailsFragment extends BaseFragment {
    private View mRootView;

    private StateLayout mStateLayout;
    private ListView mListView;
    private BusinessAdapter mAdapter;

    private ArrayList<BusinessInfoResponseBean.BusinessDetailsBean> mBusinessDetailsList;

    private static final int REQUEST_GET_BUSINESS_INFO = 0;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mRootView = inflater.inflate(R.layout.fragment_choose_add_way_child_user_right_by_business_details, container, false);
        return mRootView;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        initView();
        initEvent();
    }

    @Override
    public Animation onCreateAnimation(int transit, boolean enter, int nextAnim) {
        if (enter) {
            if (TaskCenterCommon.getInstance().isDetails()) {
                initData();
            } else {
                getBusinessInfo();
            }
        }

        return null;
    }

    private void initView() {
        mStateLayout = findView(mRootView, R.id.sl_layout);
        mListView = findView(mRootView, R.id.lv_all);
    }

    private void initData() {
        Bundle bundle = getArguments();

        if (bundle != null) {
            final int page = bundle.getInt("page");
            mBusinessDetailsList = bundle.getParcelableArrayList("dataList");

            mListView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                @Override
                public void onGlobalLayout() {
                    mListView.getViewTreeObserver().removeOnGlobalLayoutListener(this);

                    int itemHeight = mListView.getMeasuredHeight() / 10;

                    int start;
                    int end;

                    if ((page - 1) * 10 + 10 < mBusinessDetailsList.size()) {
                        start = (page - 1) * 10;
                        end = (page - 1) * 10 + 10;
                    } else {
                        start = (page - 1) * 10;
                        end = mBusinessDetailsList.size();
                    }

                    mAdapter = new BusinessAdapter(mActivity, R.layout.item_add_way_combination_details, mBusinessDetailsList.subList(start, end), itemHeight);
                    mListView.setAdapter(mAdapter);
                }
            });
        }
    }

    private void initEvent() {

    }

    public BusinessAdapter getAdapter() {
        return mAdapter;
    }

    @Override
    public void setTitle() {
        if (peekFragment() instanceof ChooseAddWayChildUseRightByBusinessDetailsFragment) {
            if (TaskCenterCommon.getInstance().isDetails()) {
                mActivity.setTitle(R.string.view_details);
            } else {
                if (TaskCenterCommon.getInstance().isStart()) {
                    mActivity.setTitle(R.string.choose_start_value);
                } else {
                    mActivity.setTitle(R.string.choose_end_value);
                }
            }
        }
    }

    @Override
    public void requestDatas() {

    }

    NoHttpUtils.OnResponseListener mOnResponseListener = new NoHttpUtils.OnResponseListener() {
        @Override
        public void onSucceed(CommonClass commonClass, int what) {
            switch (what) {
                case REQUEST_GET_BUSINESS_INFO:
                    if ("0".equals(commonClass.getCode())) {
                        BusinessInfoResponseBean businessInfoResponseBean = JSON.parseObject(commonClass.getData().toString(), BusinessInfoResponseBean.class);
                        mBusinessDetailsList = (ArrayList<BusinessInfoResponseBean.BusinessDetailsBean>) businessInfoResponseBean.getList();

                        mListView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                            @Override
                            public void onGlobalLayout() {
                                mListView.getViewTreeObserver().removeOnGlobalLayoutListener(this);

                                int itemHeight = mListView.getMeasuredHeight() / 10;

                                mAdapter = new BusinessAdapter(mActivity, R.layout.item_add_way_combination_details, mBusinessDetailsList, itemHeight);

                                mListView.setAdapter(mAdapter);
                            }
                        });
                    } else {
                        mStateLayout.showSystemView(commonClass.getMsg(), false);
                        mStateLayout.setSystemListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                mStateLayout.showProgressView(false);

                                getBusinessInfo();
                            }
                        });
                    }
                    break;
                default:
                    break;
            }
        }

        @Override
        public void onFailed(String s, int i) {
            mStateLayout.showNetworkView(false);
            mStateLayout.setNetworkListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mStateLayout.showProgressView(false);

                    getBusinessInfo();
                }
            });
        }

        @Override
        public void onFinish(int i) {
            mStateLayout.showContentView();
        }
    };

    /**
     * 查询四权 业务单位
     */
    private void getBusinessInfo() {
        mStateLayout.showProgressView(false);

        Bundle bundle = getArguments();

        if (bundle != null) {
            String name = bundle.getString(KEY_SEARCH_BY_NAME);
            String sort = bundle.getString(KEY_SEARCH_BY_SORT);

            int page = bundle.getInt("page");

            Map map = new HashMap();

            map.put("name", name);

            Map queryMap = new HashMap();
            queryMap.put("page", page);
            queryMap.put("size", 10);
            queryMap.put("sort", sort);

            map.put("query", queryMap);

            NoHttpUtils.httpPost(TaskCenterConstant.URL_GET_BUSINESS_INFO, map, mOnResponseListener, REQUEST_GET_BUSINESS_INFO);
        }
    }
}
